﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;

namespace WJ.DataSender.Service.Commen
{
    public static class ExtendMethod
    {
        /// <summary>
        /// 获取枚举值的描述信息
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string Desc(this Enum value)
        {
            Type type = value.GetType();
            string name = Enum.GetName(type, value);
            if(name is null)
            {
                return null;
            }
            FieldInfo field = type.GetField(name);
            DescriptionAttribute attribute = System.Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attribute == null)
            {
                return name;
            }
            return attribute.Description;
        }
    }
}
